package com.sgcc.operate.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.sgcc.common.core.domain.R;
import com.sgcc.common.datascope.annotation.DataScope;
import com.sgcc.operate.domain.OGroupMng;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import com.sgcc.operate.domain.ORuleAttr;
import com.sgcc.operate.service.IORuleAttrService;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.page.TableDataInfo;

/**
 * 人群规则属性Controller
 * 
 * @author sgcc
 * @date 2021-03-23
 */
@RestController
@RequestMapping("/ruleattr")
public class ORuleAttrController extends BaseController
{
    @Autowired
    private IORuleAttrService oRuleAttrService;

    /**
     * 查询人群规则属性列表
     */
    //@PreAuthorize(hasPermi = "operate:ruleattr:list")
    @GetMapping("/list")
    @DataScope(deptAlias = "o2")
    public TableDataInfo list(ORuleAttr oRuleAttr)
    {
        startPage();
        List<ORuleAttr> list = oRuleAttrService.selectORuleAttrList(oRuleAttr);
        return getDataTable(list);
    }

    /**
     * 导出人群规则属性列表
     */
    //@PreAuthorize(hasPermi = "operate:ruleattr:export")
    @Log(title = "人群规则属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @DataScope(deptAlias = "o2")
    public void export(HttpServletResponse response, ORuleAttr oRuleAttr) throws IOException
    {
        List<ORuleAttr> list = oRuleAttrService.selectORuleAttrList(oRuleAttr);
        ExcelUtil<ORuleAttr> util = new ExcelUtil<ORuleAttr>(ORuleAttr.class);
        util.exportExcel(response, list, "attr");
    }

    /**
     * 获取人群规则属性详细信息
     */
    //@PreAuthorize(hasPermi = "operate:ruleattr:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(oRuleAttrService.selectORuleAttrById(id));
    }

    /**
     * 新增人群规则属性
     */
    //@PreAuthorize(hasPermi = "operate:ruleattr:add")
    @Log(title = "人群规则属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ORuleAttr oRuleAttr)
    {
        return toAjax(oRuleAttrService.insertORuleAttr(oRuleAttr));
    }

    /**
     * 修改人群规则属性
     */
    //@PreAuthorize(hasPermi = "operate:ruleattr:edit")
    @Log(title = "人群规则属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ORuleAttr oRuleAttr)
    {
        return toAjax(oRuleAttrService.updateORuleAttr(oRuleAttr));
    }

    /**
     * 删除人群规则属性
     */
    //@PreAuthorize(hasPermi = "operate:ruleattr:remove")
    @Log(title = "人群规则属性", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(oRuleAttrService.deleteORuleAttrByIds(ids));
    }

    @PostMapping(value = "judgeQuoted")
    public AjaxResult judgeQuoted(String ids) {
        if (StringUtils.isBlank(ids)) {
            return AjaxResult.error("请选择数据后再操作！");
        }
        String[] ruleAttrIds = ids.split(",");
        for (String ruleAttrId : ruleAttrIds) {
            if (oRuleAttrService.judgeQuoted(ruleAttrId)) {
                return AjaxResult.error("选择的人群规则属性中已被引用，无法删除！");
            }
        }
        return AjaxResult.success();
    }

    /**
     * 查询群组管理列表
     */
    //@PreAuthorize(hasPermi = "operate:ruleattr:list")
    @GetMapping("/optionselect")
    @DataScope(deptAlias = "o2")
    public AjaxResult optionselect(ORuleAttr oRuleAttr)
    {
        return AjaxResult.success(oRuleAttrService.selectORuleAttrList(oRuleAttr));
    }

}
